package com.example.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MyThreadTest extends Thread {
	private Handler handler;
	private String string;
	private String result;

	public MyThreadTest(Handler handler, String string) {
		this.handler = handler;
		this.string = string;
	}

	@Override
	public void run() {

		try {
			URL u = new URL(string);
			HttpURLConnection connection = (HttpURLConnection) u.openConnection();
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream is = connection.getInputStream();
				BufferedReader bf = new BufferedReader(new InputStreamReader(is));
				String line = null;
				result = new String();
				while ((line = bf.readLine()) != null) {
					result += line;
				}
				Message msg = new Message();
				msg.what = 100;
				msg.obj = result;
				handler.sendMessage(msg);
				is.close();
				bf.close();
				connection.disconnect();

			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
